<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>倒计时的显示(Countdown display)</title>
  <style>
    #countdown {
      font-size: 20px;
      font-weight: bold;
      color: #f00;
    }
    .time {
      display: inline-block;
      width: 30px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      background-color: #000;
      color: #fff;
    }
    .split {
      color: #000;
    }
  </style>
  </style>
</head>
<body>

  <div id="countdown">
    <!-- <span class="year">03</span>
    <span class="split">-</span>
    <span class="month">25</span>
    <span class="split">-</span>
    <span class="date">43</span> -->

    <span class="time hour">03</span>
    <span class="split">:</span>
    <span class="time minute">25</span>
    <span class="split">:</span>
    <span class="time second">43</span>
  </div>

  <script>
    // 1. 获取要操作的元素
    const countdownEl = document.getElementById('countdown')
    const yearEl = countdownEl.querySelector('.year')
    const monthEl = countdownEl.querySelector('.month')
    const dateEl = countdownEl.querySelector('.date')
    const hourEl = countdownEl.querySelector('.hour')
    const minuteEl = countdownEl.querySelector('.minute')
    const secondEl = countdownEl.querySelector('.second')

    // 2. 定义倒计时时间
    const endTime = new Date()
    // endTime.setFullYear(2025)
    // endTime.setMonth(11)
    // endTime.setDate(31)

    endTime.setHours(24)
    endTime.setMinutes(0)
    endTime.setSeconds(0)
    endTime.setMilliseconds(0)

    // 3. 定义倒计时函数
    function countdown() {
      const now = new Date()
      const leftTime = endTime - now
      console.log(leftTime);
      

      // 4. 计算剩余时间
      // const year = Math.floor(leftTime / 1000 / 60 / 60 / 24 / 30 / 12) // 年份
      // const month = Math.floor(leftTime / 1000 / 60 / 60 / 24 / 30) // 月份
      // const day = Math.floor(leftTime / 1000 / 60 / 60 / 24) // 天数
      
      const hour = Math.floor(leftTime / 1000 / 60 / 60 % 24) // 小时
      const minute = Math.floor(leftTime / 1000 / 60 % 60) // 分钟
      const second = Math.floor(leftTime / 1000 % 60) // 秒

      // 5. 将剩余时间显示在页面上
      // yearEl.textContent = year
      // monthEl.textContent = padLeft(month)
      // dateEl.textContent = padLeft(day)
      
      hourEl.textContent = padLeft(hour)
      minuteEl.textContent = padLeft(minute)
      secondEl.textContent = padLeft(second)
    }

    // 6. 每秒调用一次倒计时函数
    setInterval(countdown, 1000)

    // 7. 补零函数
    function padLeft(str) {
      return str.toString().padStart(2, '0')
    }
    
  </script>
  
</body>
</html>